#!/usr/bin/env python
#coding=utf-8

import sys
import time
import click
import logging
from datetime import datetime as dt

from lib.xsl import XLSWriter
from lib.common import excel_folder_path, log_info
from lib.qyapi_weixin  import QyWeixin

from accounts import huo_accounts, bian_accounts, okex_accounts, collect_accounts, collect_bat, export_net_value
from order_oneclick import bina_order as bo,\
                           bina_cancel_order as bco,\
                           huobi_order as ho,\
                           huobi_cancel_order as hco, \
                           okex_order as oko,\
                           okexft_order as okfo,\
                           okex_cancel_order as okco,\
                           okex_future_cancel as okfc


__author__ = "Cap"

qy = QyWeixin()


@click.group()
def cli():
    pass

@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
@click.option('--quantity', '-q', help=u'交易数量')
@click.option('--price', '-p', help=u'交易价格')
def bina_order(currency, to_currency, quantity, price):
    """币安一键下单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    bo(currency, to_currency, quantity, price)


@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
def bina_cancel_order(currency, to_currency):
    """币安一键撤单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    bco(currency, to_currency)


@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
@click.option('--quantity', '-q', help=u'交易数量')
@click.option('--price', '-p', help=u'交易价格')
@click.option('--ordertype', '-o',
              type=click.Choice(['buy-market', 'sell-market', 'buy-limit', 'sell-limit', 'buy-ioc', 'sell-ioc']),
              help=u'订单类型 buy-market:市价买, sell-market:市价卖, buy-limit:限价买, sell-limit:限价卖, buy-ioc:IOC买单, sell-ioc:IOC卖单')
def huobi_order(currency, to_currency, quantity, price, ordertype):
    """火币一键下单"""
    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    ho(currency, to_currency, quantity, price, ordertype)



@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
def huobi_cancel_order(currency, to_currency):
    """火币一键撤单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    hco(currency, to_currency)


@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
@click.option('--amount', '-a', help=u'交易数量')
@click.option('--price', '-p', help=u'交易价格')
@click.option('--tradetype', '-tt',
              type=click.Choice(['buy', 'sell', 'buy_market', 'sell_market']),
              help=u'买卖类型：限价单(buy/sell) 市价单(buy_market/sell_market)')
def okex_order(currency, to_currency, amount, price, tradetype):
    """okex币币交易一键下单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    oko(currency, to_currency, amount, price, tradetype)


@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
@click.option('--contract_type', '-ct',
              type=click.Choice(['this_week', 'next_week', 'quarter']),
              help=u'合约类型: this_week:当周 next_week:下周 quarter:季度')
@click.option('--tradetype', '-tt',
              type=click.Choice(['1', '2', '3', '4']),
              help=u'交易类型: 1:开多; 2：开空; 3:平多; 4：平空')
@click.option('--price', '-p', help=u'交易价格')
@click.option('--quantity', '-q', help=u'交易数量')
def okexft_order(currency, to_currency, contract_type, tradetype, price, quantity):
    """okex合约交易一键下单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    okfo(currency, to_currency, contract_type, tradetype, price, quantity, )


@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
def okex_cancel_order(currency, to_currency):
    """okex撤销订单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    okco(currency, to_currency)


@click.command()
@click.option('--currency', '-c', help=u'币种')
@click.option('--to_currency', '-tc', help=u'交易币种')
@click.option('--contract_type', '-ct',
              type=click.Choice(['this_week', 'next_week', 'quarter']),
              help=u'合约类型: this_week:当周 next_week:下周 quarter:季度')
def okex_future_cancel(currency, to_currency, contract_type):
    """okex撤销合约订单"""

    if not currency or not to_currency:
        log_info(u'参数出错, 请检查currency或to_currency的值')
        return False

    okfc(currency, to_currency, contract_type)


cli.add_command(bina_order)
cli.add_command(bina_cancel_order)
cli.add_command(huobi_order)
cli.add_command(huobi_cancel_order)
cli.add_command(okex_order)
cli.add_command(okexft_order)
cli.add_command(okex_cancel_order)
cli.add_command(okex_future_cancel)

if __name__ == "__main__":
    try:
        logging.info('wait...')
        cli()
    except Exception as e:
        logging.exception('error_msg exit %r', e)




